package com.koron.order.workbench.todoCenter.service.impl;

import com.github.pagehelper.PageInfo;
import com.koron.bean.app.AppBean;
import com.koron.bean.base.Response;
import com.koron.bean.query.Page;
import com.koron.common.core.business.workflow.bean.param.ListParam;
import com.koron.common.core.business.workflow.bean.po.ConfigureWorkflow;
import com.koron.common.core.business.workflow.bean.po.SysWorkflow;
import com.koron.common.core.business.workflow.bean.result.FlowList;
import com.koron.common.core.business.workflow.bean.result.TaskStatistics;
import com.koron.common.core.business.workflow.service.SysWorkflowService;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.order.workbench.todoCenter.bean.dto.TodoTitleEnum;
import com.koron.order.workbench.todoCenter.bean.vo.TodoGroupVo;
import com.koron.order.workbench.todoCenter.bean.vo.TodoVo;
import com.koron.order.workbench.todoCenter.feign.ConfigureOrderFeignService;
import com.koron.order.workbench.todoCenter.service.TodoService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @ClassName TodoServiceImpl
 * @description
 * @Author zhouj
 * @Date 2022/2/11 9:11
 */
@Service
public class TodoServiceImpl implements TodoService {

    AppBean getCurrApp() {
        return ThreadLocalContext.getApp();
    }

    @Autowired
    ConfigureOrderFeignService configureOrderFeignService;

    private String getCurrAccount() {
        return ThreadLocalContext.get().getAccount();
    }

    @Autowired
    SysWorkflowService workflowService;

    @TaskAnnotation("getUndoListPage")
    @Override
    public PageInfo getUndoListPage(SessionFactory factory, ListParam listParam) {

        PageInfo<TodoVo> pageInfo = new PageInfo<>();

        List<TodoVo> undoList = getUndoList(factory, listParam);

        pageInfo.setList(undoList);

        if (CollectionUtils.isNotEmpty(undoList)) {
            pageInfo.setTotal(undoList.get(0).getTotal());
        } else {
            pageInfo.setTotal(0);
        }

        return pageInfo;
    }

    @TaskAnnotation("getUndoList")
    @Override
    public List<TodoVo> getUndoList(SessionFactory factory, ListParam listParam) {

        List<TodoVo> result = new ArrayList<>();

        Page<FlowList> flowListPage = new Page<FlowList>();

        flowListPage = workflowService.listBill(listParam);

        List<FlowList> flowList = flowListPage.getRecords();

        if (CollectionUtils.isEmpty(flowList)) {
            return new ArrayList<>();
        }

        //查询返回 工作流数据列表,组合拼接todoTitle,dataId
        List<SysWorkflow> sysWorkflows = workflowService.queryByFlowList(flowList);

        Map<String, SysWorkflow> map = new HashMap<>();

        //先按照billId整理好
        for (SysWorkflow sysWorkflow : sysWorkflows) {
            map.put(sysWorkflow.getBillId(), sysWorkflow);
        }

        for (FlowList flowInfo : flowList) {
            SysWorkflow sysWorkflow = map.get(flowInfo.getBillId());
            if (Objects.isNull(sysWorkflow)) {
                continue;
            }

            TodoVo todoVo = new TodoVo();
            BeanUtils.copyProperties(flowInfo, todoVo);
            todoVo.setDataId(sysWorkflow.getDataId());
            todoVo.setTitle(sysWorkflow.getTitle());
            todoVo.setModuleCode(sysWorkflow.getSetCode());
            if (StringUtils.isEmpty(sysWorkflow.getModuleCode())) {
                continue;
            }
            todoVo.setModuleName(sysWorkflow.getModuleCode());
            result.add(todoVo);
        }

        //这里对待办所需要的 title 进行处理
        for (TodoVo todoVo : result) {
            String example = TodoTitleEnum.getExample(todoVo.getPreOperation(), todoVo.getStartUser(), todoVo.getPreOperationUserName());
            todoVo.setTodoTitle(example + todoVo.getTitle());
            todoVo.setTotal(flowListPage.getTotal());
        }

        return result;
    }

    @TaskAnnotation("getUndoGroup")
    @Override
    public List<TodoGroupVo> getUndoGroup(SessionFactory factory, ListParam listParam) {

        //listParam取账号,没有则取当前用户
        List<String> accountList = new ArrayList<>();
        if (StringUtils.isEmpty(listParam.getOperator())) {
            accountList.add(getCurrAccount());
        }else {
            accountList.add(listParam.getOperator());
        }

        List<TodoGroupVo> todoGroupVos = new ArrayList<>();

        //分组查询各个模块的代办数量
        List<TaskStatistics> taskStatistics = workflowService.listUserTaskStatistics(null, null, "", accountList);

        //待办总数
        AtomicInteger total = new AtomicInteger();
        Map<String, Integer> map = new HashMap<>();

        if (CollectionUtils.isNotEmpty(taskStatistics)) {
            taskStatistics.forEach(p -> {
                map.put(p.getSetCode(), StringUtils.isEmpty(p.getCount()) ? 0 : Integer.parseInt(p.getCount()));
                total.addAndGet(map.get(p.getSetCode()));
            });
        }

        //查询模块的列表清单
        Response<List<ConfigureWorkflow>> listResponse = configureOrderFeignService.setCodes(ThreadLocalContext.getApp().getCode());

        List<ConfigureWorkflow> configureWorkflows = listResponse.getData();

        if (CollectionUtils.isEmpty(configureWorkflows)) {
            return todoGroupVos;
        }

        //保证模块以工作流配置的为准,只填充数值进去
        for (ConfigureWorkflow configureWorkflow : configureWorkflows) {
            TodoGroupVo todoGroupVo = new TodoGroupVo();
            todoGroupVo.setAppCode(getCurrApp().getCode());
            todoGroupVo.setAppName(getCurrApp().getName());
            todoGroupVo.setSetCode(configureWorkflow.getSetCode());
            todoGroupVo.setSetName(configureWorkflow.getModuleCode());
            todoGroupVo.setNums(map.getOrDefault(configureWorkflow.getSetCode(), 0));
            todoGroupVo.setTotal(total.get());
            todoGroupVos.add(todoGroupVo);
        }

        return todoGroupVos;

    }


}
